package com.fengmi.goods.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fengmi.goods.entity.MallGoods;
import com.fengmi.goods.entity.MallGoodsCat;
import com.fengmi.goods.entity.MallGoodsSku;
import com.fengmi.goods.mapper.MallGoodsCatMapper;
import com.fengmi.goods.mapper.MallGoodsMapper;
import com.fengmi.goods.mapper.MallGoodsSkuMapper;
import com.fengmi.goods.service.IMallGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengmi.goods.vo.HotGoodsVo;
import com.fengmi.goods.vo.SpecAndOptsVO;
import com.fengmi.vo.ResultVO;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuxm
 * @since 2022-03-02
 */
@Service
@RefreshScope
public class MallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IMallGoodsService {


    @Autowired(required = false)
    private MallGoodsCatMapper mallGoodsCatMapper;


    @Autowired
    private MallGoodsSkuMapper mallGoodsSkuMapper;

    @Value("${index.hot.topn}")
    private Integer TOPN;

    @Override
    public List<HotGoodsVo> findHotGoods() {

        //获取有商品信息的三级分类列表
        List<MallGoodsCat> mallGoodsCatList = mallGoodsCatMapper.findGoodsCatHasGoods();

        List<HotGoodsVo> hotGoodsVoList = mallGoodsCatList.stream().map(cat -> {

            HotGoodsVo hotGoodsVo = new HotGoodsVo();
            hotGoodsVo.setCat(cat);
            //查询每个分类的热卖商品
            List<MallGoods> hotGoodsList = this.baseMapper.findHotGoodsByCatId(cat.getId() + "", TOPN);
            hotGoodsVo.setGoodsList(hotGoodsList);

            return hotGoodsVo;

        }).collect(Collectors.toList());

        //

        return hotGoodsVoList;
    }

    @Override
    public List<MallGoods> findGoodsHasAuditedForEs() {



        return this.baseMapper.findGoodsHasAuditedForEs();
    }


    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public ResultVO auditGoods(String spuId) {

        MallGoods mallGoods = this.baseMapper.selectById(spuId);
        if (mallGoods == null) {
            return  new ResultVO(false,"该商品不存在");
        }

        if("1".equals(mallGoods.getAuditStatus())){
            return  new ResultVO(false,"无需重复审核");
        }

        mallGoods.setAuditStatus("1");
        mallGoods.setUpdateTime(new Date());   //同步索引库（index）

        int i = this.baseMapper.updateById(mallGoods);

        //发送消息（生成静态化页面）
        SendResult sendResult = rocketMQTemplate.syncSend("goodsgenHTML:audit", spuId);

        return new ResultVO(true,"audti success");
    }

    @Override
    public MallGoods findGoodsForHTML(String spuId) {
        //获取商品信息
        MallGoods mallGoods = this.baseMapper.selectById(spuId);
        //获取sku列表
        QueryWrapper<MallGoodsSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("is_default");
        queryWrapper.eq("spu_id",spuId);
        List<MallGoodsSku> mallGoodsSkuList = mallGoodsSkuMapper.selectList(queryWrapper);

        mallGoods.setSkuList(mallGoodsSkuList);

        //规格面板
        //[{"name":"口味","opts":["原味","咸香"]},{"name":"包装","opts":["情侣装","全家福"]}]
        List<SpecAndOptsVO> specAndOptsVOS = JSONUtil.toList(mallGoods.getSpuSpecInfo(), SpecAndOptsVO.class);


        mallGoods.setSpecAndOptsVOList(specAndOptsVOS);

        return mallGoods;
    }
}
